package com.itheima.service.impl;

import com.itheima.dao.BlogDao;
import com.itheima.pojo.Blog;
import com.itheima.service.SortService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;


@Service
public class SortServiceImpl implements SortService {
    @Autowired
    private BlogDao blogDao;

    //根据月份获取文章
    @Override
    public List<Blog> getArticleByMonth(String monthDate) {

        String month=monthDate.substring(5,7);
        String beginDate="2020-"+month+"-01";
        String endDate="2020-"+month+"-31";
        return blogDao.findBlogByMonth(beginDate,endDate);
    }

    //归档
    @Override
    public List<String> getSortList() {
        //获取当前月
        String month= DateUtils.getMonth().toString();
        //获取当前年
        String year=DateUtils.getYears().toString();
        List<String> months=new ArrayList<>();
        String begin=year+"年01月";
        String end = year +"年"+month+"月";
        Date d1=null;
        Date d2=null;
        try{
            d1=new SimpleDateFormat("yyyy年MM月").parse(begin);
            d2=new SimpleDateFormat("yyyy年MM月").parse(end);
        } catch (ParseException e){
           e.printStackTrace();
        }
        Calendar dd=Calendar.getInstance();
        dd.setTime(d1);
        while(dd.getTime().before(d2)){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy年MM月");
            String str=sdf.format(dd.getTime());
            System.out.println(str);
            dd.add(Calendar.MONTH,1);
            months.add(str);
        }
        return months;
    }
}
